//
//  LeftViewController.m
//  Meach
//
//  Created by wenshuishi on 12-11-26.
//
//

#import "LeftViewController.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import <QuartzCore/CALayer.h>
#import "NewManViewController.h"
//#import "ShiTuViewController.h"
//#import "HelpViewController.h"
#import "SettingViewController.h"
#import "MainSwipeLeftCell.h"
#import "AlwayViewController.h"
#import "VcardTableViewController.h"

NSInteger totalNewEventCount=0;

@implementation LeftViewController
@synthesize newFavMeCount,newMsgCount,datingCount,apprenticeCount,AddRequestCount,tweetCount;
@synthesize tableView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(29, 32, 36, 1);
    
    tableView = [[UITableView alloc] initWithFrame:RECT(0, 0, SCREEN_WIDTH-LEFT_DOCK_WIDTH, SCREEN_HEIGHT)];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    [tableView release];
    
    headView = [[UIView alloc] initWithFrame:RECT(0, 0, FW(tableView), 145)];
    tableView.tableHeaderView = headView;
    
    UIView *footView = [[UIView alloc] initWithFrame:RECT(0, 0, SCREEN_WIDTH, 0)];
    tableView.tableFooterView = footView;
    
    AdView = [[UIButton alloc] initWithFrame:RECT((FW(tableView)-70)/2, 30, 70, 70)];
    
    NSString *path = [RecInfo storePathWithFileName:ICUP_AVATAR];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [AdView setBackgroundImage:image forState:UIControlStateNormal];
    }else{
        [AdView setBackgroundImage:IMG(@"头像--图标.png") forState:UIControlStateNormal];
    }
    
    [AdView addTarget:self action:@selector(AdViewPressed) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:AdView];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, FY(AdView)+FH(AdView)+10, FW(tableView), 20)];
    label.textColor = [UIColor whiteColor];
//    label.text = @"海电";
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [headView addSubview:label];
    
    [NC addObserver:self selector:@selector(changeAvatar) name:ICUP_CHANGE_AVATAR object:nil];
}
-(void)changeAvatar{
    NSString *path = [RecInfo storePathWithFileName:ICUP_AVATAR];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [AdView setBackgroundImage:image forState:UIControlStateNormal];
    }else{
        [AdView setBackgroundImage:IMG(@"头像--图标.png") forState:UIControlStateNormal];
    }
}
-(void)setParentView:(UIViewController*)view{
    parentView = view;
}
-(void)AdViewPressed{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"图片选择" delegate:self cancelButtonTitle:STR(@"取消") destructiveButtonTitle:nil otherButtonTitles:STR(@"拍照"), STR(@"从手机相册选择"), nil];
    [sheet showInView:parentView.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    UIImagePickerController *imagepickerController = nil;
    if (buttonIndex == 0) {
        imagepickerController = [[UIImagePickerController alloc] init];
        imagepickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    }else if (buttonIndex == 1) {
        imagepickerController = [[UIImagePickerController alloc] init];
        imagepickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    if (imagepickerController) {
        imagepickerController.allowsEditing = NO;
        imagepickerController.delegate = self;
        [parentView presentViewController:imagepickerController animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)apicker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];//
    [AdView setBackgroundImage:[image scaleToSize:CGSizeMake(160, 160)] forState:UIControlStateNormal];
    [apicker dismissViewControllerAnimated:YES completion:^{
        NSData *uploadImageData = UIImageJPEGRepresentation(image, 0.6f);
        
        [uploadImageData writeToFile:[RecInfo storePathWithFileName:ICUP_AVATAR] atomically:YES];
    }
     ];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *profileCellIdentifier = @"ProfileCell";
    
    MainSwipeLeftCell *cell = [aTableView dequeueReusableCellWithIdentifier:profileCellIdentifier];
    if (cell == nil) {
        cell = [[[MainSwipeLeftCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:profileCellIdentifier] autorelease];
//        if (indexPath.row == 0) {
//            cell.textLabel.text = [NSString stringWithFormat:@"新手村  %@", [NSFileManager loadMyVcard].jid];
//        }
    }
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    [cell setIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 112/2;
}


- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableview deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *rootVC = nil;
    if (indexPath.row == 0) {
        rootVC = [[[NewManViewController alloc] init] autorelease];
    }else if (indexPath.row == 1) {
        rootVC = [[AlwayViewController alloc] init];
    }else if (indexPath.row == 2) {
        rootVC = [[[SettingViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    }
    if (rootVC) {
//        if (indexPath.row == 0) {
//            [[AppDelegate instance].rootViewController showViewController:rootVC];
//        }else{
            UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:rootVC] autorelease];
            [[AppDelegate instance].rootViewController showViewController:navController];
//        }
    }else{
        [[AppDelegate instance].rootViewController backToMidView];
    }
    preDidSelectRowAtIndexPathRow = indexPath.row;
}

-(void)dealloc {
    [NC removeObserver:self];
    NSLog(@"LeftViewController dealloc");
    [super dealloc];
}

@end
